home *** CD-ROM | disk | FTP | other *** search
/ Gigarom 1 / Gigarom Macintosh Archives (Quantum Leap)(CDRM1080320)(1993).iso / FILES / DEV / C-H / CMacPrimer.cpt / Timer ƒ / timer.c < prev   
C/C++ Source or Header  |  1990-03-18  |  7KB  |  365 lines

  1. #define BASE_RES_ID            400
  2. #define NIL_POINTER            0L
  3. #define MOVE_TO_FRONT        -1
  4. #define REMOVE_ALL_EVENTS    0
  5.  
  6. #define PLAIN                0
  7. #define PLAIN_ITEM            1
  8. #define BOLD_ITEM            2
  9. #define ITALIC_ITEM            3
  10. #define UNDERLINE_ITEM        4
  11. #define OUTLINE_ITEM        5
  12. #define SHADOW_ITEM            6
  13.  
  14. #define INCLUDE_SECONDS        TRUE
  15.  
  16. #define ADD_CHECK_MARK        TRUE
  17. #define REMOVE_CHECK_MARK    FALSE
  18.  
  19. #define DRAG_THRESHOLD        30
  20.  
  21. #define MIN_SLEEP            0L
  22. #define NIL_MOUSE_REGION    0L
  23.  
  24. #define WNE_TRAP_NUM        0x60
  25. #define UNIMPL_TRAP_NUM        0x9F
  26.  
  27. #define QUIT_ITEM            1
  28. #define ABOUT_ITEM            1
  29.  
  30. #define NOT_A_NORMAL_MENU    -1
  31. #define APPLE_MENU_ID        BASE_RES_ID
  32. #define FILE_MENU_ID        BASE_RES_ID+1
  33. #define FONT_MENU_ID        100
  34. #define STYLE_MENU_ID        101
  35.  
  36. #define CLOCK_LEFT            12
  37. #define CLOCK_TOP            25
  38. #define CLOCK_SIZE            18
  39.  
  40. #define ABOUT_ALERT            400
  41.  
  42. WindowPtr    gClockWindow;
  43. Boolean        gDone, gWNEImplemented;
  44. long        gCurrentTime, gOldTime;
  45. EventRecord    gTheEvent;
  46. MenuHandle    gAppleMenu, gFontMenu, gStyleMenu;
  47. int            gLastFont;
  48. Rect        gDragRect;
  49. Style        gCurrentStyle = PLAIN;
  50.  
  51.  
  52. /**************** main **************************/
  53.  
  54. main()
  55. {
  56.     ToolBoxInit();
  57.     WindowInit();
  58.     SetUpDragRect();
  59.     MenuBarInit();
  60.     MainLoop();
  61. }
  62.  
  63.  
  64. /********************* ToolBoxInit ******************/
  65.  
  66. ToolBoxInit()
  67. {
  68.     InitGraf(&thePort);
  69.     InitFonts();
  70.     FlushEvents(everyEvent, REMOVE_ALL_EVENTS);
  71.     InitWindows();
  72.     InitMenus();
  73.     TEInit();
  74.     InitDialogs(NIL_POINTER);
  75.     InitCursor();
  76. }
  77.  
  78.  
  79. /********************* WindowInit ******************/
  80.  
  81. WindowInit()
  82. {
  83.     gClockWindow = GetNewWindow(BASE_RES_ID, NIL_POINTER, MOVE_TO_FRONT);
  84.     SetPort (gClockWindow);
  85.     ShowWindow (gClockWindow);
  86.     
  87.     TextSize (CLOCK_SIZE);
  88. }
  89.  
  90. /******************** SetUpDragRect *******************/
  91.  
  92. SetUpDragRect()
  93. {
  94.     gDragRect = screenBits.bounds;
  95.     gDragRect.left += DRAG_THRESHOLD;
  96.     gDragRect.right -= DRAG_THRESHOLD;
  97.     gDragRect.bottom -= DRAG_THRESHOLD;
  98. }
  99.  
  100. /************************ MenuBarInit ********************/
  101.  
  102. MenuBarInit()
  103. {
  104.     Handle    myMenuBar;
  105.     
  106.     myMenuBar = GetNewMBar (BASE_RES_ID);
  107.     SetMenuBar(myMenuBar);
  108.     gAppleMenu = GetMHandle (APPLE_MENU_ID);
  109.     gFontMenu = GetMenu (FONT_MENU_ID);
  110.     gStyleMenu = GetMenu (STYLE_MENU_ID);
  111.     
  112.     InsertMenu (gFontMenu, NOT_A_NORMAL_MENU);
  113.     AddResMenu (gFontMenu, 'FONT');
  114.     InsertMenu (gStyleMenu, NOT_A_NORMAL_MENU);
  115.     
  116.     CheckItem (gStyleMenu, PLAIN_ITEM, TRUE);
  117.     AddResMenu (gAppleMenu, 'DRVR');
  118.     DrawMenuBar();
  119.     
  120.     gLastFont = 1;
  121.     HandleFontChoice (gLastFont);
  122. }
  123.  
  124. /******************** MainLoop ***************************/
  125.  
  126. MainLoop()
  127. {
  128.     gDone = FALSE;
  129.     gWNEImplemented = (NGetTrapAddress(WNE_TRAP_NUM, ToolTrap) !=
  130.                         NGetTrapAddress (UNIMPL_TRAP_NUM, ToolTrap));
  131.     while (gDone == FALSE)
  132.     {
  133.         HandleEvent();
  134.     }
  135. }
  136.  
  137. /********************** HandleEvent ************************/
  138.  
  139. HandleEvent ()
  140. {
  141.     char theChar;
  142.     
  143.     if (gWNEImplemented)
  144.         WaitNextEvent (everyEvent, &gTheEvent, MIN_SLEEP,
  145.                         NIL_MOUSE_REGION);
  146.     else
  147.     {
  148.         SystemTask();
  149.         GetNextEvent(everyEvent,&gTheEvent);
  150.     }
  151.     
  152.     switch (gTheEvent.what)
  153.     {
  154.         case nullEvent:
  155.             HandleNull();
  156.             break;
  157.         case mouseDown:
  158.             HandleMouseDown();
  159.             break;
  160.         case keyDown:
  161.         case autoKey:
  162.             theChar = gTheEvent.message & charCodeMask;
  163.             if (( gTheEvent.modifiers & cmdKey) != 0)
  164.                     HandleMenuChoice(MenuKey(theChar));
  165.             break;
  166.         case updateEvt:
  167.             BeginUpdate (gTheEvent.message);
  168.             EndUpdate(gTheEvent.message);
  169.             break;
  170.     }
  171. }
  172.  
  173. /***************** HandleNull **********************/
  174.  
  175. HandleNull()
  176. {
  177.     GetDateTime (&gCurrentTime);
  178.     if (gCurrentTime != gOldTime)
  179.     {
  180.         DrawClock(gClockWindow);
  181.     }
  182. }
  183.  
  184. /********************* DrawClock ***********************/
  185.  
  186. DrawClock(theWindow)
  187. WindowPtr    theWindow;
  188. {
  189.     Str255    myTimeString;
  190.     
  191.     IUTimeString (gCurrentTime, INCLUDE_SECONDS, myTimeString);
  192.     EraseRect(&(theWindow->portRect));
  193.     MoveTo(CLOCK_LEFT, CLOCK_TOP);
  194.     DrawString(myTimeString);
  195.     gOldTime = gCurrentTime;
  196. }
  197.  
  198. /************************ HandleMouse ******************/
  199.  
  200. HandleMouseDown()
  201. {
  202.     WindowPtr     whichWindow;
  203.     short int    thePart;
  204.     long int    menuChoice, windSize;
  205.     
  206.     thePart = FindWindow (gTheEvent.where, &whichWindow);
  207.     switch (thePart)
  208.     {
  209.         case inMenuBar:
  210.             menuChoice = MenuSelect (gTheEvent.where);
  211.             HandleMenuChoice(menuChoice);
  212.             break;
  213.         case inSysWindow:
  214.             SystemClick (&gTheEvent, whichWindow);
  215.             break;
  216.         case inDrag:
  217.             DragWindow(whichWindow,gTheEvent.where, &gDragRect);
  218.             break;
  219.         case inGoAway:
  220.             gDone = TRUE;
  221.             break;
  222.     }
  223. }
  224.  
  225. /***************** HandleMenuChoice ***************/
  226.  
  227. HandleMenuChoice(menuChoice)
  228. long int    menuChoice;
  229. {
  230.     int    theMenu;
  231.     int    theItem;
  232.     
  233.     if (menuChoice !=0)
  234.     {
  235.         theMenu = HiWord (menuChoice);
  236.         theItem = LoWord (menuChoice);
  237.         switch(theMenu)
  238.         {
  239.             case APPLE_MENU_ID:
  240.                 HandleAppleChoice(theItem);
  241.                 break;
  242.             case FILE_MENU_ID:
  243.                 HandleFileChoice(theItem);
  244.                 break;
  245.             case FONT_MENU_ID:
  246.                 HandleFontChoice(theItem);
  247.                 break;
  248.             case STYLE_MENU_ID:
  249.                 HandleStyleChoice(theItem);
  250.                 break;
  251.         }
  252.         HiliteMenu(0);
  253.     }
  254. }
  255.  
  256. /********************* HandleAppleChoice *************/
  257.  
  258. HandleAppleChoice(theItem)
  259. int    theItem;
  260. {
  261.     Str255        accName;
  262.     int            accNumber;
  263.     short int    itemNumber;
  264.     DialogPtr    AboutDialog;
  265.     
  266.     switch (theItem)
  267.     {
  268.         case ABOUT_ITEM:
  269.             NoteAlert(ABOUT_ALERT, NIL_POINTER);
  270.             break;
  271.         default:
  272.             GetItem(gAppleMenu,theItem,accName);
  273.             accNumber = OpenDeskAcc (accName);
  274.             break;
  275.     }
  276. }
  277.  
  278. /**************** HandleFileChoice ***********************/
  279.  
  280. HandleFileChoice (theItem)
  281. int    theItem;
  282. {
  283.     switch (theItem)
  284.     {
  285.         case QUIT_ITEM:
  286.             gDone = TRUE;
  287.             break;
  288.     }
  289. }
  290.  
  291. /********************** HandleFontChoice ******************/
  292.  
  293. HandleFontChoice (theItem)
  294. int    theItem;
  295. {
  296.     int        fontNumber;
  297.     Str255    fontName;
  298.     
  299.     CheckItem (gFontMenu, gLastFont, REMOVE_CHECK_MARK);
  300.     CheckItem (gFontMenu, theItem, ADD_CHECK_MARK);
  301.     gLastFont = theItem;
  302.     GetItem (gFontMenu, theItem, fontName);
  303.     GetFNum (fontName,&fontNumber);
  304.     TextFont (fontNumber);
  305. }
  306.  
  307. /************************* HandleStyleChoice *****************/
  308.  
  309. HandleStyleChoice(theItem)
  310. int    theItem;
  311. {
  312.     switch(theItem)
  313.     {
  314.         case PLAIN_ITEM:
  315.             gCurrentStyle = PLAIN;
  316.             break;
  317.         case BOLD_ITEM:
  318.             if (gCurrentStyle & bold)
  319.                 gCurrentStyle -= bold;
  320.             else
  321.                 gCurrentStyle |= bold;
  322.             break;
  323.         case ITALIC_ITEM:
  324.             if (gCurrentStyle & italic)
  325.                 gCurrentStyle -= italic;
  326.             else
  327.                 gCurrentStyle |= italic;
  328.             break;
  329.         case UNDERLINE_ITEM:
  330.             if (gCurrentStyle & underline)
  331.                 gCurrentStyle -= underline;
  332.             else
  333.                 gCurrentStyle |= underline;
  334.             break;
  335.         case OUTLINE_ITEM:
  336.             if (gCurrentStyle & outline)
  337.                 gCurrentStyle -= outline;
  338.             else
  339.                 gCurrentStyle |= outline;
  340.             break;
  341.         case SHADOW_ITEM:
  342.             if (gCurrentStyle & shadow)
  343.                 gCurrentStyle -= shadow;
  344.             else
  345.                 gCurrentStyle |= shadow;
  346.             break;
  347.     }
  348.     CheckStyles();
  349.     TextFace(gCurrentStyle);
  350. }
  351.  
  352. /************************ CheckStyles ********************/
  353.  
  354. CheckStyles()
  355. {
  356.     CheckItem (gStyleMenu, PLAIN_ITEM, gCurrentStyle == PLAIN);
  357.     CheckItem (gStyleMenu, BOLD_ITEM, gCurrentStyle & bold);
  358.     CheckItem (gStyleMenu, ITALIC_ITEM, gCurrentStyle & italic);
  359.     CheckItem (gStyleMenu, UNDERLINE_ITEM, gCurrentStyle & underline);
  360.     CheckItem (gStyleMenu, OUTLINE_ITEM, gCurrentStyle & outline);
  361.     CheckItem (gStyleMenu, SHADOW_ITEM, gCurrentStyle & shadow);
  362. }
  363.     
  364.         
  365.